<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {


    public function index(){
	   $goods=M('goods');
	   import('ORG.Util.Page');
	   if(isset($_POST['chaname'])){$condition['name']=$_POST['chaname'];}//获取post提交过来的条件值
       $count=$goods->where($condition)->count();
	   $Page       = new Page($count,8);
	   $nowPage = isset($_GET['p'])?$_GET['p']:1;
	   $list=$goods->order('id desc')->where($condition)->limit($Page->firstRow.','.$Page->listRows)->select();
	   $Page->setConfig('theme', '%upPage%  %linkPage%  %downPage%');
	   $show       = $Page->show();
	   $config=array(
	     'url'=>'http://localhost/shop/home/Tpl',
	   );
	   $this->assign('config',$config);
	   $this->assign('page',$show);
	   $this->assign('list',$list);
       $this->display();
     
    }
	public function tplchange(){
	
	  $this->display();
	}
	
	public function show(){
	   $showid=$_GET['id'];
	   $goods=M('goods');
	   if(isset($_GET['id'])){$condition['id']=$_GET['id'];}//获取post提交过来的条件值	   
	   $list=$goods->where($condition)->select();
	   $config=array(
	     'tittle'=>'商品展示',
	   );
	   	   
	   $this->assign('pageconfig',$config);
	   $this->assign('list',$list);
	   $this->display();
	}
	public function shop(){  //购物车页面
       $id=$_GET['id'];
	   session_start();
	   ob_start();
       $shopcar=@$_SESSION['shopcar'];
	   if(is_array($_SESSION['shopcar'])){
	     $shopcar[$id]=array("id"=>$id,"num"=>1);
	   }else{
	     $shopcar[$id]=array("id"=>$id,"num"=>1);
	   }$_SESSION['shopcar']=$shopcar;     //购物车
	   $db=M("goods");   //调取数据库
	   $list[36]=$db->where('id=36')->select();
	   foreach($shopcar as $index=>$value){
	       $condition['id']=$index;
	       $list[$index]=$db->where($condition)->select();
	   }
	   $config=array(//页面参数
	     'url'=>'http://localhost/shop/home/Tpl',
		 'list'=>$list,
	   );
	 $this->assign('config',$config);
	 $this->assign('shop',$shopcar);
	 $this->display();
	}
	public  function shopajax(){  //购物车更改
	   $id=$_POST['id'];
	   $action=$_POST['action'];
	   $ajaxnum=$_POST['num'];
	   session_start();
	   ob_start();
	     if($action=="del"){
		  unset($_SESSION['shopcar'][$id]);
		 }elseif($action=="editnum"){
		  $_SESSION['shopcar'][$id]['num']=$ajaxnum;
		 }elseif($action=="clear"){
		    $_SESSION['shopcar']='';
		 }
	}
	public function login(){             //登录页面
	 $errmsg='';
	 $username=$_POST['username'];
	 $userpsd=md5($_POST['userpsd']);
	 $conn=M("reg");
	 $condition['user_name']= $username;
	 $login=$conn->where($condition)->select();
	 $loginpsd=$login[0]['user_password'];
	    if(!empty($username)&&!empty($userpsd)){
		   if($loginpsd==$userpsd){
		   session_start();
		   $_SESSION['loginname']=$username;
		   $_SESSION['loginstate']=1;
		   cookie('loginname',$username);
	       //header("Location:".__ROOT__);
		   echo'<script> window.history.go(-2);</script>';
	      }else{
		     $errmsg='
			    <script>
				   $(document).ready(function(){
	      $(".login_wrap_warn").html("<b></b>请输入正确的用户名和密码！");
		  $(".login_wrap_warn").css({"background":"#FFEBEB","border":"1px solid #E4393C","color":"#E4393C"});
		  $(".login_wrap_warn b").css({"backgroundPosition":"-104px -48px"});
				   });
				</script>
			 ';
		  }
		}
	 $pageconfig=array(
	    "tittle"=>"欢迎登录",
	 );
	 $this->assign('errmsg',$errmsg);
	 $this->assign('username',cookie('loginname'));
	 $this->assign('pageconfig',$pageconfig);
	 $this->display();
	}
	
	public function outlogin(){
	   session_start();
	   unset($_SESSION['loginname']);
	   $_SESSION['loginstate']=0;
	  $root=__URL__."/login";
	  header("location:".$root);
	}
	
	
	public function reg(){       //注册页面
	 $user=D("reg");
	 $_POST['userpsd']=md5($_POST['userpsd']);
     $user->create($_POST);
	 $user->add();	
	 $pageconfig=array(
	    "tittle"=>"--注册用户",
	 );
	// print_r($_POST);
	 $this->assign('pageconfig',$pageconfig);
	 $this->display();
	}
	public function check_name(){
	    $username=$_POST['username'];
	   $user=M("reg");
	   $num=$user->where('user_name="'.$username.'"')->count();
	   echo $num;
	}
	
	public function shiyan(){
	  $array=array('a','b','c');
	  $this->assign('hello',$array);
	  $this->display();
	}
	
}
?>